package actionScripts.commands
{
	import actionScripts.LoginModelLocator;
	import actionScripts.business.GlobalDelegates;
	import actionScripts.common.ClsGlobal;
	import actionScripts.events.CommonEvent;
	import actionScripts.events.LoginDataEvent;
	import actionScripts.utils.AppConstant;
	import actionScripts.utils.Utils;
	import actionScripts.vo.LoggedInUser;
	
	import mx.controls.Alert;
	import mx.rpc.Responder;

	public class GlobalRemoteServiceCalls
	{
		//-------------------------
		// PRIVATE VARIABLES
		//-------------------------
		private var appModelLocator : LoginModelLocator = LoginModelLocator.getInstance();
		private var anEventObject 	: Object;
		public function GlobalRemoteServiceCalls(){
		}
		//-------------------------
		// EXECUTE
		//-------------------------
		public function execute( event:LoginDataEvent ) : void {
			var aLoginEvent	    : LoginDataEvent = event;
			anEventObject  		= aLoginEvent.data;
			var aDelegate		: GlobalDelegates;
			var aResponder		: Responder;
			var userName 		: String;
			switch( aLoginEvent.type ) {				
				case LoginDataEvent.VALIDATE_USER:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( loginResultHandler, loginFaultHandler );
					aDelegate = new GlobalDelegates( aResponder );
					
					// assigning userName and password to the delegate to send with Remote service
					// in other calls we have the username and password with us after login
					aDelegate.userName = anEventObject.userName;
					aDelegate.password = anEventObject.password;
					aDelegate.login(anEventObject.userName,anEventObject.password);
					break;
				case LoginDataEvent.CHECK_USERNAME:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( checkUserResultHandler, checkUserFaultHandler );
					aDelegate = new GlobalDelegates( aResponder );
					userName = anEventObject as String;
	    			aDelegate.checkUserNameAvailiablity(userName);
					break;
				case LoginDataEvent.REGISTER_USER:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( registerUserResultHandler, registerUserFaultHandler );
					aDelegate = new GlobalDelegates( aResponder );
					aDelegate.registerUser(appModelLocator.usrDetail);
					break;
				case LoginDataEvent.FORGOT_PASSWORD:
					ClsGlobal.showLoader( true, false, false );
					aResponder = new Responder( forgotPasswordResultHandler, forgotPasswordFaultHandler );
					aDelegate = new GlobalDelegates( aResponder );
					userName  = anEventObject as String;
					aDelegate.forgotPassword(userName);
					break;
				
			}		
		}
		
		//------------------------------------------------------------
		// LOGIN RESULT/FAULT HADNLERS
		//------------------------------------------------------------
		public function loginResultHandler( event:Object ) : void {
			//remove layer from the stage
			ClsGlobal.removeLoader();
			appModelLocator.loggedUser = event.result as LoggedInUser;
			appModelLocator.dispatchEvent( new CommonEvent( CommonEvent.VALIDATE_USER_EVENT, null ) );
		}
		
		public function loginFaultHandler( event:Object ) : void {
			//remove layer from the stage
			ClsGlobal.removeLoader();
		}
		
		public function checkUserResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var isavailiable:Boolean = false;;
			isavailiable = event.result;
			appModelLocator.dispatchEvent( new CommonEvent( CommonEvent.CHECK_USER_EVENT, isavailiable ) );
		}
		public function checkUserFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		public function registerUserResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var result:String = event.result as String;
			Utils.setViewIndex(AppConstant.LOGIN_VIEW);
		}
		
		public function registerUserFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
		public function forgotPasswordResultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
			var result:String = event.result as String;
			Alert.show(""+result);
		}
		public function forgotPasswordFaultHandler( event:Object ) : void {
			ClsGlobal.removeLoader();
		}
	}
}